package com.lst.demo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lst.demo.db.LstDbActivity;
import com.lst.demo.dialog.DialogActivity;
import com.lst.demo.ui.LockViewSetActivity;
import com.lst.demo.ui.PictureActivity;
import com.lst.demo.ui.TestHandlerActivity;
import com.lst.demo.ui.UnLockActivity;
import com.lst.demo.ui.WebActivity;
import com.lst.demo.utils.FingerPrintUtils;
import com.lst.demo.utils.wpsutils.FileOpen;
import com.lst.demo.view.CustomDialog;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //设置手机音量
        setVolum();
    }

    //直接设置手机的音量
    private void setVolum() {
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//
//        mAudioManager.setStreamVolume(
//                // index 系统
//                AudioManager.STREAM_SYSTEM,// streamType
//                mAudioManager
//                        .getStreamMaxVolume(AudioManager.STREAM_SYSTEM),
//                AudioManager.FLAG_ALLOW_RINGER_MODES);// flag

        mAudioManager.setStreamVolume(
                // index 媒体
                AudioManager.STREAM_MUSIC,// streamType
                mAudioManager
                        .getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                AudioManager.FLAG_ALLOW_RINGER_MODES);// flag

        mAudioManager.setStreamVolume(
                // index  闹钟
                AudioManager.STREAM_ALARM,// streamType
                mAudioManager
                        .getStreamMaxVolume(AudioManager.STREAM_ALARM),
                AudioManager.FLAG_PLAY_SOUND);// flag

        mAudioManager.setStreamVolume(
                // index  通知
                AudioManager.STREAM_NOTIFICATION,// streamType
                mAudioManager
                        .getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
                AudioManager.FLAG_PLAY_SOUND);// flag


        mAudioManager.setStreamVolume(
                // index  铃声
                AudioManager.STREAM_RING,// streamType
                mAudioManager
                        .getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);// flag
    }

    public void startDb(View view) {
        Log.i(TAG, "startDb: 开启数据库页面");
        startActivity(new Intent(this, LstDbActivity.class));
    }

    public void startRecycleSwitch(View view) {
        Log.i(TAG, "startRecycleSwitch: 开启ReycleView切换页面");
        startActivity(new Intent(this, PictureActivity.class));
    }


    public void startDialog(View view) {

        startActivity(new Intent(this, DialogActivity.class));
    }


    public void startHandler(View view) {

        startActivity(new Intent(this, TestHandlerActivity.class));
    }

    /**
     * 打开文件
     *
     * @param view
     */
    public void openFiles(View view) {

        String fielpath = PathUtils.getExternalStoragePath() + "/test/laodong.doc";
        LogUtils.e("文件路径:" + fielpath);
        if (!FileUtils.isFileExists(fielpath)) {
            ToastUtils.showLong("文件不存在");
            return;
        }
        File file = new File(fielpath);


        String mimeTypeFromFile = FileOpen.getMimeTypeFromFile(file);
        LogUtils.i("文件类型:" + mimeTypeFromFile);

        if (mimeTypeFromFile.startsWith("image")) {
            //是图片类型
            FileOpen.openFile2(MainActivity.this, file);
        } else {
            //不是图片,调用wps
            FileOpen.openWpsNew(MainActivity.this, fielpath);
        }


    }


    public void web(View view) {

        ActivityUtils.startActivity(WebActivity.class);
    }

    //设置手势图案
    public void setunlock(View view) {

        ActivityUtils.startActivity(LockViewSetActivity.class);

    }


    //手势解锁
    public void unlock(View view) {

        String lockpaswd = SPUtils.getInstance().getString("lockpaswd");
        if (TextUtils.isEmpty(lockpaswd)) {

            ToastUtils.showLong("请先设置解锁图案");

            ActivityUtils.startActivity(LockViewSetActivity.class);

        } else {
            ActivityUtils.startActivity(UnLockActivity.class);

        }
    }


    //指纹解锁相关
    private CustomDialog dialog;
    private TextView tvFingerInfo;

    public void finger(View view) {


        final FingerPrintUtils fingerPrintUtils = new FingerPrintUtils(this);
        if (fingerPrintUtils.supportFingerprint()) {
            fingerPrintUtils.start(new FingerPrintUtils.FingerPrintResult() {
                @Override
                public void success() {
                    tvFingerInfo.setText("解锁成功");
                    tvFingerInfo.setTextColor(Color.parseColor("#00ff00"));

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (dialog != null) {
                                dialog.dismiss();
                            }
                        }
                    }, 400);

                }

                @Override
                public void error(int code, CharSequence info) {
                    dialog.startAnim(MainActivity.this, tvFingerInfo, "尝试次数过多,请稍候重试");

                }

                @Override
                public void retry(int code, CharSequence info) {
                    dialog.startAnim(MainActivity.this, tvFingerInfo, "指纹验证失败,请重试");

                }
            });

            CustomDialog.Builder builder = new CustomDialog.Builder(this);
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int id = view.getId();
                    if (id == R.id.tv_other) {
                        fingerPrintUtils.cancelCallback();
                        dialog.dismiss();
                        ActivityUtils.startActivity(UnLockActivity.class);

                    } else if (id == R.id.fingerprint_cancel_tv) {
                        fingerPrintUtils.cancelCallback();
                        dialog.dismiss();
                    }

                }
            };

            dialog = builder
                    .style(R.style.Dialog)
                    .heightDimenRes(R.dimen.dilog_identitychange_height)
                    .widthDimenRes(R.dimen.dilog_identitychange_width)
                    .cancelTouchout(false)
                    .cancelAble(false)
                    .view(R.layout.finger_layout)
                    .addViewOnclick(R.id.fingerprint_cancel_tv, listener)
                    .addViewOnclick(R.id.tv_other, listener)
                    .build();

            tvFingerInfo = dialog.getView().findViewById(R.id.tv_finger_info);
            dialog.show();


        }


    }


}
